#include "worldmodel.hpp"

void ReleaseObject(Object* obj){
    switch(obj->GetType()){
        case ObjType::UNKNOWN:
            delete obj;
            SDL_Log("released a unknown object");
            break;
        case ObjType::CREATURE:
            delete dynamic_cast<Creature*>(obj);
            break;
        case ObjType::WALL:
            delete dynamic_cast<Solid*>(obj);
            break;
        case ObjType::GROUND:
            delete dynamic_cast<Ground*>(obj);
            break;
        case ObjType::WEAPON:
            delete dynamic_cast<Weapon*>(obj);
            break;
        case ObjType::ARMOR:
            delete dynamic_cast<Armor*>(obj);
            break;
        default:
            SDL_Log("{%s:%d}:object can't be delete", __FILE__, __LINE__);
            assert(false);
    }
}

WorldModel::WorldModel():_cur_layer(nullptr){
}

void WorldModel::RegistObject(Object* object){
    if(object->GetType()==ObjType::CREATURE){
        _creatures.push_back(dynamic_cast<Creature*>(object));
    }else{
        _objects.push_back(object);
    }
}

void WorldModel::RegistLayer(LayerModel* layer){
    if(layer){
        _layers.push_back(layer);
    }else{
        SDL_Log("layer is nullptr");
    }
}

void WorldModel::ChangeLayer(LayerModel* layer){
    _cur_layer = layer;
}

LayerModel* WorldModel::GetLayer(){
    return _cur_layer;
}

void WorldModel::DeleteAll(){
    for(Object* o : _objects){
        ReleaseObject(o);
    }
    _objects.clear();
    for(LayerModel* layer : _layers)
        delete layer;
    _layers.clear();
}

// FIXME 现在地图只能绘制在左上方，后面需要修改
void WorldModel::Draw(){
    if(_cur_layer)
        _cur_layer->Draw(0, 0);

    // 确保_player总是在所有块的最上面
    for(Creature* creature : _creatures)
        creature->Draw();
}

void WorldModel::Update(){
    for(Object* obj : _objects)
        obj->Update();
    _cur_layer->Update();
}

WorldModel::~WorldModel(){
    DeleteAll();
}

/* @brief 画出玩家的信息 */
void DrawPlayerInfo(){
    SDL_Color color = {255,255,255,255};
    DrawText("| 玩家：桂明谦",50,1,color);
    DrawText("| 生命值(hp):"+to_string(player->properties_.hp_),50,3,color);
    int hp_len = player->properties_.hp_/5;
    for (int i = 0;i<hp_len;i++){
        DrawText(">", 59+i,3,{0,255,0,255});
    }
    DrawText("| 魔力值(mp):"+to_string(player->properties_.mp_),50,5,color);
    int mp_len = player->properties_.mp_/5;
    for (int i=0;i<mp_len;i++){
        DrawText(">",59+i,5,{0,0,255,255}); 
    }
    DrawText("| 攻击程度(attack):"+to_string(player->properties_.attack_),50,7,color);
    DrawText("| 防御力(defence):"+to_string(player->properties_.defence_),65,7,color);
    DrawText("| 饥饿度(hungry):"+to_string(player->properties_.hungry_),50,9,color);
    DrawText("| 速度(speed):"+to_string(player->properties_.speed_),65,9,color);
    DrawText("| 力量(strength):"+to_string(player->properties_.strength_),50,11,color);
    DrawText("| 敏捷(dex):"+to_string(player->properties_.dex_),65,11,color);
    DrawText("| 智力(intelli):"+to_string(player->properties_.intelli_),50,13,color);
    DrawText("| 负重(burden):"+to_string(player->properties_.burden_),65,13,color);
};

WorldModel worldmodel;
